//
//  MyAuthCodeViewController.m
//  parent
//
//  Created by 闫珍 on 15/8/5.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "MyAuthCodeViewController.h"
#import "AFAppClient.h"
#import "SettingPwdViewController.h"

@interface MyAuthCodeViewController ()
{
    int count_num;
    NSTimer *timer;
}
@property (weak, nonatomic) IBOutlet UITextField *authcode;
@property (weak, nonatomic) IBOutlet UIButton *tipslabel;

@end

@implementation MyAuthCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"注册";
    count_num=60;
    [self CustomBackButton];
}
-(void)timeHandler
{
    if (count_num >=0) {
        _tipslabel.titleLabel.text = [NSString stringWithFormat:@"短信验证码已发送( %d )",count_num];
        [_tipslabel setTitle:[NSString stringWithFormat:@"短信验证码已发送( %d )",count_num ] forState:UIControlStateNormal];
        count_num--;
    }else{
        _tipslabel.titleLabel.text = @"重新获取验证码";
        [_tipslabel setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    }
}
-(void)viewDidDisappear:(BOOL)animated
{
    //停下timer
    [timer invalidate];
}

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"did appear.");
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeHandler) userInfo:nil repeats:YES];
}
//重新获取验证码
- (IBAction)reGetAuthCode:(id)sender {
    //当时间小于60的时候，可以再次倒计时获取验证码。
    if (count_num < 0) {
        count_num = 60;
    }
}
- (IBAction)sendCode:(id)sender {
    NSString *txt = self.authcode.text;
    if([txt isEqualToString:@""]){
        [self showHudComplete:@"验证码不能为空" success:NO];
        return;
    }
    NSDictionary *params = @{@"phone_num":_phone,@"verify_code":txt};
    [self showHud:@"正在发送" isDim:YES];
    [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kSMSVerify] withParameters:params success:^(NSDictionary *responseObject) {
        if([[responseObject objectForKey:@"code"]integerValue]==1){
            [self hideHud2];
            SettingPwdViewController *pwd = [[SettingPwdViewController alloc]init];
            pwd.phone = _phone;
            pwd.code = txt;
            [self.navigationController pushViewController:pwd animated:YES];
        }else if([[responseObject objectForKey:@"code"]integerValue]==0){
            
            [self showHudComplete:@"验证码错误，请重新输入" success:NO];
        }

    } failure:^(NSError *error) {
        [self showHudComplete:@"网络连接失败" success:NO];
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
